//
//  MainTabBar.m
//  YKZB
//
//  Created by 曾少伟 on 2017/3/7.
//  Copyright © 2017年 曾少伟. All rights reserved.
//

#import "MainTabBar.h"

@interface MainTabBar ()
@property (nonatomic,strong)UIImageView *bgImgView;

@property (nonatomic,strong)UIButton *lastItem;

@property (nonatomic,strong)UIButton *launchItem;

@end

@implementation MainTabBar
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

#pragma mark - events
- (void)itemClick:(UIButton *)sender {
    debugMethod();
    if ([self.delegate respondsToSelector:@selector(mainTabBar:clickItem:)]) {
        [self.delegate mainTabBar:self clickItem:sender.tag - 100];
    }
    
    self.lastItem.selected = NO;
    self.lastItem = sender;
    self.lastItem.selected = YES;
    
    if (sender.tag == 100 + TabBarItemTypeLaunch) return;
    float time = 0;
    __block typeof (time) wt = time;
    [UIView animateKeyframesWithDuration:0.5 delay:0 options:0 animations:^{
        wt += 0.2;
        [UIView addKeyframeWithRelativeStartTime:wt relativeDuration:0 animations:^{
            self.lastItem.transform = CGAffineTransformMakeScale(1.2, 1.2);
        }];
        wt += 0.3;
        [UIView addKeyframeWithRelativeStartTime:wt relativeDuration:0 animations:^{
            self.lastItem.transform = CGAffineTransformIdentity;
        }];
    } completion:nil];
}


#pragma mark - view
- (void)createSubviews {
    
    self.bgImgView = ({
        UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"global_tab_bg"]];
        [self addSubview:imgView];
        imgView;
    });
    
    NSArray *btnImgNameArr = @[@"tab_live",@"tab_me"];
    int i = 0;
    for (NSString *btnImgName in btnImgNameArr) {
        CGFloat btnW = self.width/btnImgNameArr.count;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:btnImgName] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:[btnImgName stringByAppendingString:@"_p"]] forState:UIControlStateSelected];
        btn.adjustsImageWhenHighlighted = NO;//高亮阴影
        if (i == 0) {
            btn.selected = YES;
            self.lastItem = btn;
        }
        btn.tag = 100 + i;
        [btn addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake(i * btnW, 0, btnW, self.height);
        [self addSubview:btn];
        i ++;
    }
    
    self.launchItem = ({
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"tab_launch"] forState:UIControlStateNormal];
        [btn sizeToFit];
        [btn addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + TabBarItemTypeLaunch;
        btn.centerX = self.centerX;
        btn.centerY = -3;
        [self addSubview:btn];
        btn;
    });
    
}

@end
